<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.SubstoreDAO" %>

<%@page import="db.Project"%>
<%@page import="db.Material"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>

<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	SubstoreDAO substoreDAO = (SubstoreDAO)ctx.getBean("SubstoreDAO");
	
	int tid = (Integer)session.getAttribute("tid");
	
	String substore = request.getParameter("substore");
	String procode = request.getParameter("procode");
	String proname = request.getParameter("proname");
	String zprocode = request.getParameter("zprocode");
	String zproname = request.getParameter("zproname");
	String manager = request.getParameter("manager");
	
	String type = request.getParameter("type");
	request.setAttribute("type",type);
	String message = "";
	
	
	if(type=="2" || "2".equals(type)){
		if(procode==null || "".equals(procode)){
				message = "添加项目信息失败，项目编码不能为空。";
				request.setAttribute("message",message);
				request.setAttribute("suess",false);
				pageContext.forward("/vm/prinfo/add_prinfo.vm");
				return;
		}
			
			List li = projectDAO.findByCode(procode);
			if(li!=null && li.size()>0){
			//System.out.println("===========>size="+li.size());
				for(int lint=0;lint<li.size();lint++){
					Project project = (Project)li.get(lint);
					if(project.getIsDelete()==0){
						//System.out.println("===========>project="+project.getIsDelete());
						message = "添加项目信息失败，项目编码已经存在。";
						request.setAttribute("message",message);
						request.setAttribute("suess",false);
						pageContext.forward("/vm/prinfo/add_prinfo.vm");
						return;
					}
				}
			}
		
		
		
		
		//List md = materialDao.findCodeAndTeamId(codename,tid);
		if(proname==null || "".equals(proname)){
				message = "添加项目信息失败，项目名称不能为空。";
				request.setAttribute("message",message);
				request.setAttribute("suess",false);
				pageContext.forward("/vm/prinfo/add_prinfo.vm");
				return;
		}
		int sub = 1;
		if(substore!=null && !"".equals(substore)){
			sub = Integer.parseInt(substore);
		}else{
			sub = 1;
		}
		
		
		if(zprocode==null){
			zprocode="";
		}
		
		if(zproname==null){
			zproname="";
		}
		if(manager==null){
			manager="";
		}
		Project pr = new Project();
		pr.setCode(procode);
		pr.setIsDelete(0);
		pr.setSubstoreId(sub);
		pr.setName(proname);
		pr.setSubCode(zprocode);
		pr.setSubName(zproname);
		pr.setManager(manager);
		pr.setDate(new Date());
		pr.setTeamId(tid);
		projectDAO.save(pr);
		message = "恭喜您！添加项目信息成功!";
		request.setAttribute("message",message);
		request.setAttribute("suess",true);
		pageContext.forward("/vm/prinfo/add_prinfo.vm");
	}else{
		List list = substoreDAO.findAll();
		request.setAttribute("substore",list);
		pageContext.forward("/vm/prinfo/add_prinfo.vm");
	}
	
%>
